package socket;

import cn.hutool.core.util.HexUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;


public class DaHuaIPCamera {
    public DaHuaIPCamera() {

        Thread t = new CameraInfoThread();
        t.start();
    }

    public static void main(String[] args) {
        new DaHuaIPCamera();
    }

}

class CameraInfoThread extends Thread {

    InetSocketAddress inetSocketAddress = new InetSocketAddress("192.168.13.77", 37810);
    String strCameraInfo = "20000000444849500000000000000000490000000000000049000000000000007b20226d6574686f6422203a20224448446973636f7665722e736561726368222c2022706172616d7322203a207b20226d616322203a2022222c2022756e6922203a2031207d207d0a";
    DatagramSocket datagramSocket = null;

    public void run() {

        try {

            if (datagramSocket == null) {
                datagramSocket = new DatagramSocket(37810);
            }

            while (true) {

                byte[] bytes = HexUtil.decodeHex(strCameraInfo);
                DatagramPacket packet = new DatagramPacket(bytes, bytes.length, inetSocketAddress);
                datagramSocket.send(packet);

                bytes = new byte[1024];

                packet = new DatagramPacket(bytes, bytes.length);
                datagramSocket.receive(packet);
                String m = new String(bytes, 0, packet.getLength(), "utf-8");
                System.out.println("message:" + m);

                sleep(3000L);
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }
}